package com.example.chessbattleplatform.web.chess_rooms.Pojo.websocket;


import com.example.chessbattleplatform.Pojo.WebSocketSessionIdAndUserId;
import com.example.chessbattleplatform.utils.WebSocketSessionSendMessage;
import com.example.chessbattleplatform.web.chess_rooms.Pojo.Vo.RoomsLimitVo;
import com.example.chessbattleplatform.web.chess_rooms.Pojo.dto.ListQueriesWebSocketDto;
import com.example.chessbattleplatform.web.chess_rooms.service.impl.RoomsServiceInterface;
import jakarta.websocket.Session;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

//列表查询 房间信息 存储所有的session

@Slf4j
public class ListQueriesSessionSendMessage extends WebSocketSessionSendMessage {


    // 存储 每个用户的 列表查询数据 并存储到 session
    private final Map<String, ListQueriesWebSocketDto> ListQueriesWebSocketDtoSessionMap = new HashMap<>();

    // 添加
    public WebSocketSessionIdAndUserId addSessionMap(Session session, ListQueriesWebSocketDto listQueriesWebSocketDto, WebSocketSessionIdAndUserId webSocketSessionIdAndUserId) {
        ListQueriesWebSocketDtoSessionMap.put(session.getId(), listQueriesWebSocketDto);

        return super.addSessionMap(session, webSocketSessionIdAndUserId);
    }
    // 更新 ListQueriesWebSocketDtoSessionMap
    public void updateSessionMap(Session session, ListQueriesWebSocketDto listQueriesWebSocketDto) {
        ListQueriesWebSocketDtoSessionMap.put(session.getId(), listQueriesWebSocketDto);
    }

    // 删除
    @Override
    public WebSocketSessionIdAndUserId deleteSessionMap(Session session) {
        ListQueriesWebSocketDtoSessionMap.remove(session.getId());
        return super.deleteSessionMap(session);
    }


    // 群发 列表房间数据
    public void sendListQueriesMessages(RoomsServiceInterface roomsService) {
        ListQueriesWebSocketDtoSessionMap.forEach((key, value) -> {
            RoomsLimitVo roomsLimitVo = roomsService.listRooms(value.getCurrent(), value.getLimit());
            super.sendSuccessMessage(roomsLimitVo, key);
        });
    }

    // 指定发送 id 并发送,列表房间数据
    public void sendListQueriesMessage(Session session, RoomsServiceInterface roomsService) {
        //获取 ListQueriesWebSocketDto
        ListQueriesWebSocketDto listQueriesWebSocketDto = ListQueriesWebSocketDtoSessionMap.get(session.getId());
        // 获取 列表房间数据
        RoomsLimitVo roomsLimitVo = roomsService.listRooms(listQueriesWebSocketDto.getCurrent(), listQueriesWebSocketDto.getLimit());
        log.info(" 发送列表房间数据: {}", roomsLimitVo);
        super.sendSuccessMessage(roomsLimitVo, session);
    }
}
